/**
 * Created by Jimmy on 16/9/23.
 */
//获取设备可以使用的宽度
var deviceWidth = window.screen.availWidth;
var boardSize = 0.92 * deviceWidth;  //棋盘大小
var boardSpace = 0.04 * deviceWidth; //边距
var boardCell = 0.18 * deviceWidth;//棋格


//获取top
function getTopPos(i, j) {
    return boardSpace + i * (boardCell + boardSpace);
}

//获取left
function getLeftPos(i, j) {
    return boardSpace + j * (boardCell + boardSpace);
}

//获取文字颜色
function getNumberBackgroundColor(number) {
    switch (number) {
        case 2:
            return "#eee4da";
        case 4:
            return "#ede0c8";
        case 8:
            return "#f2b179";
        case 16:
            return "#f59563";
        case 32:
            return "#f67e5f";
        case 64:
            return "#f65e3b";
        case 128:
            return "#edcf72";
        case 256:
            return "#edcc61";
        case 512:
            return "#99cc00";
        case 1024:
            return "#33b5e5";
        case 2048:
            return "#09c";
        case 4096:
            return "#a6c";
        case 8192:
            return "#93c";
    }
    return "black";
}

//获取棋格背景色
function getNumberTextColor(number) {
    if (number == 2 || number == 4) {
        return "776e65";
    }
    return "white";
}

//棋盘是否有空间
function noSpace(board) {
    for (var i = 0; i < 4; i++) {
        for (var j = 0; j < 4; j++) {
            if (board[i][j] == 0)
                return false;
        }
    }
    return true;
}

//棋格是否可以向左移动
function canMoveLeft(board) {
    for (var i = 0; i < 4; i++) {
        for (var j = 1; j < 4; j++) { //不用判断第一列的情况
            //可以向左移动的条件
            //条件1:左侧数字是0
            //条件2:左侧的数字和当前数字一样大
            if (board[i][j] != 0)
                if (board[i][j - 1] == 0 || board[i][j - 1] == board[i][j])
                    return true;
        }
    }
    return false;
}

//棋格是否可以向右移动
function canMoveRight(board) {
    for (var i = 0; i < 4; i++) {
        for (var j = 0; j < 3; j++) { //不用判断最后一列的情况
            //可以向上移动的条件
            //条件1:右侧数字是0
            //条件2:右侧的数字和当前数字一样大
            if (board[i][j] != 0)
                if (board[i][j + 1] == 0 || board[i][j + 1] == board[i][j])
                    return true;
        }
    }
    return false;
}


//棋格是否可以向上移动
function canMoveUp(board) {
    for (var i = 1; i < 4; i++) {
        for (var j = 0; j < 4; j++) { //不用判断第一行的情况
            //可以向上移动的条件
            //条件1:上方数字是0
            //条件2:上方数字和当前数字一样大
            if (board[i][j] != 0)
                if (board[i - 1][j] == 0 || board[i - 1][j] == board[i][j])
                    return true;
        }
    }
    return false;
}

//棋格是否可以向下移动
function canMoveDown() {
    for (var i = 0; i < 3; i++) {
        for (var j = 0; j < 4; j++) { //不用判断最后一行的情况
            //可以向上移动的条件
            //条件1:下方数字是0
            //条件2:下方数字和当前数字一样大
            if (board[i][j] != 0)
                if (board[i + 1][j] == 0 || board[i + 1][j] == board[i][j])
                    return true;
        }
    }
    return false;
}

//横向移动是否有非0的数字阻碍
function noBlockHorizontal(row, column1, column2, board) {
    for (var i = column1 + 1; i < column2; i++) {
        if (board[row][i] != 0)
            return false;
    }
    return true;
}

//纵向移动是否有非0的数字阻碍
function noBlockVertical(column, row1, row2, board) {
    for (var i = row1 + 1; i < row2; i++) {
        if (board[i][column] != 0)
            return false;
    }
    return true;
}

//没有棋格可以移动
function noMove(board) {
    if (canMoveLeft(board) || canMoveUp(board) || canMoveRight(board) || canMoveDown(board))
        return false;
    return true;
}
